/*
 * Copyright (c) 2010, University of Innsbruck, Austria.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package at.sti2.sepn.pattern.network.alpha;

import org.apache.log4j.Logger;

import at.sti2.sepn.core.triple.RDFLiteral;
import at.sti2.sepn.core.triple.RDFURIReference;
import at.sti2.sepn.pattern.network.WorkingMemoryElement;

public class ConstantTestAlphaNode extends AlphaNode {

	public enum FildToTest {SUBJECT, PREDICATE, OBJECT, NOTEST};
	
	public FildToTest fildToTest = null;
	public String lexicalTestValue = null;
	
	static Logger logger = Logger.getLogger(ConstantTestAlphaNode.class);
	
	public ConstantTestAlphaNode(FildToTest fildToTest, String lexicalTestValue) {
		super();
		
		this.fildToTest = fildToTest;
		this.lexicalTestValue = lexicalTestValue;
	}
	
	public void testActivation(WorkingMemoryElement wme){
		
		String lexicalNodeValue = null;
		
		//If there is something to test
		if (fildToTest != FildToTest.NOTEST){
			//Take the node value
			if (fildToTest == FildToTest.SUBJECT){
				if (wme.getTriple().getSubject() instanceof RDFURIReference)
					lexicalNodeValue = ((RDFURIReference)wme.getTriple().getSubject()).getValue().toString();
			} else if (fildToTest == FildToTest.PREDICATE){
				lexicalNodeValue = ((RDFURIReference)wme.getTriple().getPredicate()).getValue().toString();
			} else {
				if (wme.getTriple().getObject() instanceof RDFURIReference)
					lexicalNodeValue = ((RDFURIReference)wme.getTriple().getObject()).getValue().toString();
				else if (wme.getTriple().getObject() instanceof RDFLiteral)
					lexicalNodeValue = ((RDFLiteral)wme.getTriple().getObject()).getValue();
				
			}
			
			//Test is failing
			if (!lexicalNodeValue.equals(lexicalTestValue))
				return;
		}
		
		if (outputMemory != null)
			outputMemory.activate(wme);
		
		for (AlphaNode child : children)
			child.testActivation(wme);
	}

	public FildToTest getFildToTest() {
		return fildToTest;
	}

	public void setFildToTest(FildToTest fildToTest) {
		this.fildToTest = fildToTest;
	}

	public String getLexicalTestValue() {
		return lexicalTestValue;
	}

	public void setLexicalTestValue(String lexicalTestValue) {
		this.lexicalTestValue = lexicalTestValue;
	}
}
